﻿using System;
using System.Diagnostics;
using System.IO;

namespace BrnMall.Core
{
    /// <summary>
    /// BrnMall搜索管理类
    /// </summary>
    public class BMASearch
    {
        private static ISearchStrategy _isearchstrategy = null;//搜索策略

        static BMASearch()
        {
            try
            {
                string[] fileNameList = Directory.GetFiles(System.Web.HttpRuntime.BinDirectory,
                    "BrnMall.SearchStrategy.*.dll", SearchOption.TopDirectoryOnly);
                Trace.TraceInformation(string.Join("-", fileNameList));
                var strategyName = fileNameList[0].Substring(fileNameList[0].IndexOf("SearchStrategy.",StringComparison.InvariantCultureIgnoreCase) + 15)
                    .Replace(".dll", "");
                Trace.TraceInformation(strategyName);
                var strategyType =
                    Type.GetType(
                        string.Format("BrnMall.SearchStrategy.{0}.SearchStrategy, BrnMall.SearchStrategy.{0}",
                            strategyName),
                        true,
                        true);
                Trace.TraceInformation(string.Format(
                    "BrnMall.SearchStrategy.{0}.SearchStrategy, BrnMall.SearchStrategy.{0}",
                    strategyName));
                if (strategyType == null) Trace.TraceInformation("反射类型为空");
                if (strategyType != null) _isearchstrategy = (ISearchStrategy)Activator.CreateInstance(strategyType);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                Trace.TraceError(e.StackTrace);
                Trace.TraceError(
                    "创建'搜索策略对象'失败,可能存在的原因:未将'搜索策略对象'添加到bin目录中;'搜索策略对象'文件名不符合'BrnMall.SearchStrategy.{策略名称}.dll'格式");

                throw new BMAException(
                    "创建'搜索策略对象'失败,可能存在的原因:未将'搜索策略对象'添加到bin目录中;'搜索策略对象'文件名不符合'BrnMall.SearchStrategy.{策略名称}.dll'格式");

            }
        }

        /// <summary>
        /// 搜索策略实例
        /// </summary>
        public static ISearchStrategy Instance
        {
            get { return _isearchstrategy; }
        }
    }
}
